/**
 * @文件名称: TermQuartz.java
 * @版权声明 : Copyright (C) 2008-2016 北斗天汇（北京）科技有限公司
 * @创建人员: 徐斌
 * @文件描述:
 * @修改时间: 2017年6月16日 上午8:58:04
 * @修改历史： 2017年6月16日
 */

package com.bdth.controller;

import com.bdth.model.PageModel;
import com.bdth.model.Role;
import com.bdth.model.RoleFuncKey;
import com.bdth.model.User;
import com.bdth.service.IFuncConfigService;
import com.bdth.service.IRoleFuncService;
import com.bdth.service.IRoleService;
import com.bdth.service.IUserService;
import com.bdth.utils.HtmlUtil;
import com.bdth.utils.SessionUtils;
import com.bdth.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IFuncConfigService funcConfigService;

	@Autowired
	private IRoleFuncService roleFuncService;

	@Autowired
	private IUserService userService;

	/**
	 * 用户首页
	 *
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request)
			throws Exception {
		return forword("/role/roleList");
	}

	/**
	 * json 列表页面
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(PageModel pageModel, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String roleName = request.getParameter("roleName");
		map.put("roleName", roleName);
		//紧跟着的第一个查询方法会被分页
		PageHelper.startPage(pageModel.getPage(), pageModel.getRows());
		List<Role> dataList = roleService.queryByMapList(map);
		//获得结果集条总数
		PageInfo pageList = new PageInfo(dataList);
		//设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", pageList.getTotal());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}


	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response)
			throws Exception {
		Map<String, Object> context = new HashMap<String, Object>();
		Role bean = roleService.queryById(id);
		if (bean == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}

		String strFuncMenu = "";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", id);
		List<RoleFuncKey> menuList = roleFuncService.queryByMapList(map);
		if (menuList != null && menuList.size() > 0) {
			for (RoleFuncKey roleFuncKey : menuList) {
				strFuncMenu += roleFuncKey.getFuncID() + ",";
			}
		}

		context.put("strFuncMenu", strFuncMenu);
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}


	/**
	 * 添加或修改数据
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public void save(Role bean, HttpServletResponse response, String strFuncMenu)
			throws Exception {
		Map<String, Object> context = new HashMap<String, Object>();

		if (StringUtil.isEmptyWithNullStr(strFuncMenu)) {
			sendFailureMessage(response, "请选择功能权限!");
			return;
		}

		List<Integer> menuIds = new ArrayList<Integer>();
		String[] funcIdArray = strFuncMenu.split(",");
		for (String str : funcIdArray) {
			int funcId = Integer.parseInt(str);
			menuIds.add(funcId);
		}

		if (bean.getId() == null) {
			bean.setCreateDate(new Date());
			roleService.add(bean, menuIds);
		} else {
			roleService.update(bean, menuIds);
		}

		sendSuccessMessage(response, "保存成功~");

	}


	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletResponse response)
			throws Exception {
		roleService.delete(id);
		sendSuccessMessage(response, "删除成功");
	}


	/**
	 * 假删除
	 *
	 * @param id
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/fakeDelete")
	public void fakeDelete(Integer[] id, HttpServletResponse response)
			throws Exception {

		for (int i : id) {

			Map<String, Object> map = new HashMap<String, Object>();
			List<Integer> roleIds = new ArrayList<>();
			roleIds.add(i);
			map.put("roleIds", roleIds);
			List<User> userList = userService.queryByMapList(map);
			if (userList != null && userList.size() > 0) {
				sendFailureMessage(response, "该角色下分配有用户，不能删除!");
				break;
			}

			Role role = new Role();
			role.setId(i);
			role.setDeleted(1);
			roleService.deleteFake(role);
			sendSuccessMessage(response, "删除成功");
		}

	}


	@RequestMapping("/loadRoleList")
	public void loadRoleList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = SessionUtils.getUser(request);
		//去除超级管理员
		if (user.getRoleID() != 1) {
			map.put("id", 1);
		}
		List<Role> roloList = roleService.queryByMapList(map);
		HtmlUtil.writerJson(response, roloList);
	}

}
